<?php

/**
 * Description of Question
 *
 * @author asus
 */
class Questionmodel extends CI_Model {

    //add a question
    public function add_question($parent_id = -99, $visible = HIDDEN, $content = null) {
        $save_question = array(
            'parent_id' => $parent_id,
            'visible' => $visible,
            'content' => $content,
            'is_locked' => UNLOCKED
        );

        $save = $this->db->insert('question', $save_question);
        return $save;
    }

    //delete a question
    public function delete_question($question_id = null) {
        if ($question_id !== null)
            $this->db->delete('question', array('question_id' => $question_id));
        return $this->db->affected_rows();
    }

    //update a question
    public function update_question($question_id = null, $parent_id = null, $visible = null, $content = null, $is_locked = null) {
        if ($question_id !== null) {

            $stack = array();

            if ($parent_id !== null) {
                $stack['parent_id'] = $parent_id;
            }
            if ($visible !== null) {
                $stack['visible'] = $visible;
            }
            if ($content !== null) {
                $stack['content'] = $content;
            }
            if ($is_locked !== null) {
                $stack['is_locked'] = $is_locked;
            }

            if ($stack !== null) {
                $this->db->where('question_id', $question_id);
                return $this->db->update('question', $stack);
            }
        }
    }

    //get all question
    function get_all_question($where = null, $order_by = null, $limit = 10000, $offset = 0) {
        $this->db->from('question');

        $this->db->where('question_id !=', 0);
        if ($where !== null)
            $this->db->where($where);
        if ($order_by !== null)
            $this->db->order_by($order_by);

        $this->db->limit($limit);
        $this->db->offset($offset);

        $query = $this->db->get();

        return $query;
    }

    //get a question
    function get_info_question($question_id = null) {
        if ($question_id !== null) {
            return $this->get_all_question(array('question_id' => $question_id), null, 1);
        }
    }

    //get all visible question --> return array of array containing sorted visible record based on parent_id
    public function get_all_visible_question($order_by = null, $limit = 10000, $offset = 0) {

        $query_result = $this->get_all_question(array('visible' => VISIBLE), 'parent_id asc', $limit, $offset);

        if ($query_result->num_rows() > 0) {
            $first_row = $query_result->row();
            if ($first_row->parent_id == ROOT_PARENT_ID) {

                $initial_question_id = $first_row->question_id;
                $initial_parent_id = ROOT_PARENT_ID;


                $newArr = array();

                for ($i = 0; $i < $query_result->num_rows(); ++$i) {

                    foreach ($query_result->result() as $row) {
                        if ($row->question_id == $initial_question_id) {

                            $initial_parent_id = $initial_question_id;

                            $tempArr = array(
                                'question_id' => $row->question_id,
                                'parent_id' => $row->parent_id,
                                'visible' => $row->visible,
                                'content' => $row->content,
                                'date_created' => $row->date_created,
                                'is_locked' => $row->is_locked
                            );

                            array_push($newArr, $tempArr);
                            break;
                        }
                    }

                    foreach ($query_result->result() as $row1) {
                        if ($row1->parent_id == $initial_parent_id) {
                            $initial_question_id = $row1->question_id;
                            break;
                        }
                    }
                }

                return $newArr;
            }
            return 'error';
        } else {
            return array();
        }
    }

    public function get_sum_all_visible_question() {
        $query = $this->db->get_where('question', array('visible' => VISIBLE));
        return $query->num_rows();
    }

    //return start date of a question
    public function get_startdate_question($question_id) {
        return $this->get_date_question($question_id, 'asc');
    }

    //return end date of a question
    public function get_enddate_question($question_id) {
        return $this->get_date_question($question_id, 'desc');
    }

    //
    private function get_date_question($question_id, $order = 'asc') {
        if ($question_id !== null) {
            $this->db->select('respond.*, customer.*', FALSE);
            $this->db->from('respond');
            $this->db->where('question_id', $question_id);
            $this->db->join('customer', 'respond.customer_id = customer.customer_id', 'LEFT OUTER');
            $this->db->order_by('datetime', $order);
            $respon = $this->db->get()->row();

            return $respon;
        }
    }

    public function get_countsatisfy_question($question_id = null, $startdate = null, $enddate = null, $upto_enddate = true, $plasa_id = null, $csr_id = null) {
        return $this->get_count_question($question_id, $startdate, $enddate, 1, $upto_enddate, $plasa_id, $csr_id);
    }

    public function get_countunsatisfy_question($question_id = null, $startdate = null, $enddate = null, $upto_enddate = true, $plasa_id = null, $csr_id = null) {
        return $this->get_count_question($question_id, $startdate, $enddate, 0, $upto_enddate, $plasa_id, $csr_id);
    }

    private function get_count_question($question_id = null, $startdate = null, $enddate = null, $satisfy_level = 0, $upto_enddate = true, $plasa_id = null, $csr_id = null) {
        if (($question_id !== null) && ($startdate !== null) && ($enddate !== null)) {

            $this->db->select('respond.satisfy_level', FALSE);
            $this->db->from('respond');

            $enddate_bound = ' <= ';
            if ($upto_enddate)
                $enddate_bound = ' < ';

            $where_clause = array(
                'respond.question_id' => $question_id,
                'respond.satisfy_level' => $satisfy_level,
                'customer.datetime >= ' => $startdate,
                'customer.datetime' . $enddate_bound => $enddate,
            );
			
            if(!is_null($csr_id))  $where_clause['`customer`.`csr_id`'] = $csr_id;
			else {
				if(!is_null($plasa_id))  $where_clause['`csr`.`plasa_id`'] = $plasa_id;
			}
            

            $this->db->where($where_clause);
            $this->db->join('customer', 'respond.customer_id = customer.customer_id', 'INNER JOIN');
            if(!is_null($plasa_id) && is_null($csr_id)) $this->db->join('csr', 'csr.csr_id = customer.csr_id', 'INNER JOIN');
			
            $respon = $this->db->get()->num_rows();
            return $respon;
        }
    }

    public function set_lock($question_id = null) {
        if ($question_id !== null) {
            $this->update_question($question_id, null, null, null, LOCKED);
        }
    }

    public function is_locked($question_id = null) {
        $query_result = $this->get_info_question($question_id);
        foreach ($query_result->result() as $row) {
            return $row->is_locked;
        }
    }

}

?>
